package main

import (
	"../backend"
	"../mysql"
	"fmt"
)

func main() {
	c, addr, user, pass := newTestConn()

	// select @@global.binlog_checksum

	// Return True if binlog-checksum = CRC32. Only for MySQL > 5.6
	checksum, err := c.CheckSumEnable("SHOW GLOBAL VARIABLES LIKE 'BINLOG_CHECKSUM'")
	if err != nil {
		fmt.Println(err)
	}

	//fmt.Println(checksum)
	// If checksum is enabled we need to inform the server about the that
	// we support it
	if checksum == true {
		var com = "set @master_binlog_checksum= @@global.binlog_checksum"
		res, err := c.Execute(com)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(res)
	}

	binlogFileNameV, err := c.ShowMasterStatusOld("show master status")

	if err != nil {
		panic(err)
	}

	var binlogPosition uint32 = 120
	var binlogFlag uint16 = 0 //0
	var serverId uint32 = 698148981
	// var binlogFileName = "mysql-bin.000126"

	var binlogFileName = []byte(binlogFileNameV)
	length := len(binlogFileName)

	data := make([]byte, 4+2+4+length)

	//4
	data[0] = byte(binlogPosition)
	data[1] = byte(binlogPosition >> 8)
	data[2] = byte(binlogPosition >> 16)
	data[3] = byte(binlogPosition >> 24)

	//2
	data[4] = byte(binlogFlag)
	data[5] = byte(binlogFlag >> 8)

	//4
	data[6] = byte(serverId)
	data[7] = byte(serverId >> 8)
	data[8] = byte(serverId >> 16)
	data[9] = byte(serverId >> 24)

	//fix
	copy(data[4+2+4:], binlogFileName)

	c.BinlogDumpOld(mysql.COM_BINLOG_DUMP, data, addr, user, pass)

	//if err != nil {
	//	fmt.Println(err)
	//}
	//fmt.Println(result)
	//if result[0] == mysql.OK_HEADER {
	//	fmt.Println("true============")
	//}

}

func newTestConn() (*backend.Conn, string, string, string) {
	c := new(backend.Conn)

	addr := "172.16.50.188:3309"
	user := "mysqlsync"
	pass := "mysqlsync"

	if err := c.Connect(addr, user, pass, ""); err != nil {
		fmt.Println("Connect failed ____________________")
	} else {
		fmt.Println("Connect success ____________________")
	}

	return c, addr, user, pass
}
